﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
namespace SpyTxT
{
    /*
     * 异步向服务器post数据
     */
    class HttpCommit
    {
        public static String cookieid = "";
        public static void PostTo(String url, String postdata)
        {
            System.Net.ServicePointManager.Expect100Continue = false;
            try
            {  
               
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
                myRequest.Method = "POST";
                myRequest.ContentType = "application/x-www-form-urlencoded";
                myRequest.ContentLength = data.Length;

                Stream newStream = myRequest.GetRequestStream();

                newStream.Write(data, 0, data.Length);
                newStream.Close();

                myRequest.BeginGetResponse(new AsyncCallback(OnPostResponse), myRequest);
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                System.Console.WriteLine(url);
                System.Console.WriteLine(postdata);
            }
          
        }
        public static void OnPostResponse(IAsyncResult ar)
        {
            HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(ar);
            Stream stream = response.GetResponseStream();
            StreamReader sr = new StreamReader(stream, Encoding.UTF8);
            Console.WriteLine(sr.ReadToEnd());
            response.Close();
        }

        public static void PostToMyServer(string tocity, string fromcity, string contact, string tel, string content)
        {
            string postdata = "tocity=" + tocity + "&fromcity=" + fromcity + "&contact=" + contact + "&tel=" + tel + "&content=" + content;

            byte[] data = Encoding.UTF8.GetBytes(postdata);
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://su112wei.vicp.net:8081/phproot/input.php");
            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.ContentLength = data.Length;
            
            Stream newStream = myRequest.GetRequestStream();

            newStream.Write(data, 0, data.Length);
            newStream.Close();

            myRequest.BeginGetResponse(new AsyncCallback(OnMyServerResponse), myRequest);

            

        }
        static void OnMyServerResponse(IAsyncResult ar)
        {
            HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(ar);
            Stream stream = response.GetResponseStream();
            StreamReader sr = new StreamReader(stream, Encoding.UTF8);
            Console.WriteLine(sr.ReadToEnd());
        }
        public static String GetTheirServer()
        {
            if (cookieid == "")
            {
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://test.ny-dt.com/fmp/wlrData.list.action");
                myRequest.Method = "GET";
                myRequest.BeginGetResponse(new AsyncCallback(OnGetMyTheirResponse), myRequest);
            }

            return cookieid;
            //byte[] data = Encoding.UTF8.GetBytes(postdata);
            //HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://test.ny-dt.com/fmp/wlrData.list.action");
            //myRequest.Method = "GET";
            //myRequest.ContentType = "application/x-www-form-urlencoded";
            //myRequest.ContentLength = data.Length;

            //Stream newStream = myRequest.GetRequestStream();

            //newStream.Write(data, 0, data.Length);
            //newStream.Close();

            //myRequest.BeginGetResponse(new AsyncCallback(OnMyTheirResponse), myRequest);
        }
        public static void PostTheirServer(String start_sheng, String start_chengshi, String start_diqu, String arrive_sheng, String arrive_city, String arrive_diqu,String content,String datetime,String contact,String tel)
        {
            //if (cookieid != "")
            {
                string postdata = "";
                postdata += "wlrData.type=%E8%B4%A7%E6%BA%90";
                postdata += "&wlrData.startProvince=" + start_sheng;
                postdata += "&wlrData.startCity=" + start_chengshi;
                postdata += "&wlrData.startRegion=" + start_diqu;
                postdata += "&wlrData.endProvince=" + arrive_sheng;
                postdata += "&wlrData.endCity=" + arrive_city;
                postdata += "&wlrData.endRegion=" + arrive_diqu;
                postdata += "&wlrData.content=" + content;
                postdata += "&wlrData.time=" + datetime;
                postdata += "&wlrData.contact=" + contact;
                postdata += "&wlrData.phone=" + tel;


                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://test.ny-dt.com/fmp/wlrData.save.action");
                myRequest.Method = "POST";
                myRequest.ContentType = "application/x-www-form-urlencoded";
                myRequest.ContentLength = data.Length;
                //myRequest.Headers.Add("Cookie",cookieid);
                Stream newStream = myRequest.GetRequestStream();

                newStream.Write(data, 0, data.Length);
                newStream.Close();

                myRequest.BeginGetResponse(new AsyncCallback(OnPostMyTheirResponse), myRequest);

            }
            

           
        }
        public static void OnPostMyTheirResponse(IAsyncResult ar)
        {

            HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(ar);
            Stream stream = response.GetResponseStream();
            StreamReader sr = new StreamReader(stream, Encoding.UTF8);
            Console.WriteLine(sr.ReadToEnd());
        }
        public static void OnGetMyTheirResponse(IAsyncResult ar)
        {
            HttpWebRequest request = (HttpWebRequest)ar.AsyncState;

            
            
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(ar);
            String cookie = response.Headers["Set-Cookie"];

            //Stream stream = response.GetResponseStream();
            //StreamReader sr = new StreamReader(stream, Encoding.UTF8);
            //Console.WriteLine(sr.ReadToEnd());



            cookieid = cookie.Substring(0, cookie.IndexOf(';') - 1);
            System.Console.WriteLine(cookieid);



        }

    }
}
